from django.db import models
from users.models import User
from goods.models import Good
# Create your models here.


# 收货地址表
# receiver varchar(20)
# receiver_mobile varchar(11)
# receiver_addr varchar(100)
# user fk
class Addr(models.Model):
    receiver = models.CharField("收货人", max_length=20)
    receiver_mobile = models.CharField("收货手机号", max_length=11)
    receiver_addr = models.CharField("收货地址", max_length=100)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户")

    def __str__(self):
        return "收件人：%s" % self.receiver

    class Meta:
        db_table = "addr"
        verbose_name_plural = "收货地址"


# 订单信息表
# order_id  varchar(64) pk  '订单编号'
# user  关系fk   '下单用户'
# addr  '收货地址'
# total_count int '商品总数'
# total_amount  decimal(10,2)  '总价格'
# freight  decimal(10,2)  '运费'
# pay_method smallint  choices  '支付方式'
# (1, '支付宝'),
# (2,'微信支付'),
# (3,'银联支付'),
# (4,'货到付款')
# status smallint  '支付状态'
# 1-待支付，2-待发货，3-待收货，4-待评价， 5-已完成
# created_time  订单创建时间


class OrderInfo(models.Model):
    """订单信息"""
    PAY_METHOD = (
        (1, '支付宝'),
        (2, '微信支付'),
        (3, '银联支付'),
        (4, '货到付款')
    )
    PAY_STATUS = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成'),
    )
    order_id = models.CharField(max_length=64, primary_key=True, verbose_name="订单编号")
    user = models.ForeignKey(User, related_name='orders', on_delete=models.CASCADE, verbose_name="下单用户")
    addr = models.ForeignKey(Addr, on_delete=models.CASCADE, verbose_name="收货地址")
    total_count = models.IntegerField(default=1, verbose_name="商品总数")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="总价格")
    freight = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="运费")
    pay_method = models.SmallIntegerField(default=1, choices=PAY_METHOD, verbose_name="支付方式")
    # 1-待支付，2-待发货，3-待收货，4-待评价，5-已完成
    status = models.SmallIntegerField(default=1, choices=PAY_STATUS, verbose_name="订单状态")
    created_time = models.DateTimeField(verbose_name="下单时间", auto_now_add=True)

    class Meta:
        db_table = "order_info"
        verbose_name_plural = '订单信息'

    def __str__(self):
        return self.order_id


# 订单商品表
# sku 购买的商品  fk  '订单商品'
# count  int  '购买的数量'
# price decimal(10,2) '价格'
# order 所属订单
# comment text '评论信息'
# score  smallint  '评分'  默认5
# is_anonymous  '是否匿名评价'
# is_commented '是否评价'
class OrderGoods(models.Model):
    """订单商品"""
    # 购买的商品
    sku = models.ForeignKey(Good, on_delete=models.CASCADE, verbose_name="订单商品")
    count = models.IntegerField(default=1, verbose_name="购买的数量")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="价格")
    order = models.ForeignKey(OrderInfo, related_name='orderGoods', on_delete=models.CASCADE, verbose_name="所属订单")
    comment = models.TextField(default="", verbose_name="评价信息")
    score = models.SmallIntegerField(default=5, verbose_name='评分')
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否评价')

    class Meta:
        db_table = "order_goods"
        verbose_name_plural = '订单商品'

    def __str__(self):
        return "%s:%s"%(self.order.order_id,self.sku.name)


# 支付信息
# trade_id   varchar(100) 唯一 可为空 '支付编号'
# order 订单fk
class PaymentInfo(models.Model):
    """支付信息"""
    trade_id = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="支付编号")
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name='订单')

    def __str__(self):
        return self.trade_id

    class Meta:
        db_table = 'payment_info'
        verbose_name_plural = '支付信息'


